package com.yy.train.ticket.utils;

import java.util.concurrent.locks.ReentrantLock;

public class DemoTest1 {
    private static int count = 0; //定义count = 0
    private static final ReentrantLock lock = new ReentrantLock();//创建 lock 实例
    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) { //通过for循环创建两个线程
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //每个线程让count自增100次
                    for (int i = 0; i < 100; i++) {
                        try {
                            lock.lock(); //调用 lock 方法
                            count++;
                        } finally {
                            lock.unlock(); //调用unlock方法释放锁
                        }
                    }
                }
            }). start();
        }
        try{
            Thread.sleep(2000);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(count);
    }
}

